<template>
  <div class="table-wrap" v-loading="loading">
    <div class="search_header">
      <div class="search_header_left">
        <!-- <el-button
          icon="el-icon-download"
          @click="downloadFile"
          v-has="'leaderApprove:export'"
          >下载数据</el-button
        > -->
      </div>

      <div class="search_header_right">
        <el-input
          placeholder="请输入交办内容"
          v-model="parameter.keywords"
          @keydown.enter.native="search"
          clearable
          @clear="search"
        >
          <span
            @click="search"
            slot="suffix"
            style="display: inline-block"
          >
            <Icon
              title="搜索"
              name="sousuo"
              color="#A7ADB5"
              font-size="20"
            />
          </span>
        </el-input>
      </div>
    </div>
    <el-table
      :data="projectList"
      :header-cell-style="{
        background: '#F2F6FB',
        color: ' #6B767D'
      }"
      :row-class-name="tableRowClassName"
    >
      <el-table-column
        label="序号"
        align="center"
        show-overflow-tooltip
        width="80"
      >
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="交办对象"
        prop="sendUserName"
        show-overflow-tooltip
      />
      <el-table-column
        label="交办内容"
        prop="sendContent"
        show-overflow-tooltip
      />
      <!-- <el-table-column
        label="来源"
        prop="source"
        show-overflow-tooltip
      /> -->
      <el-table-column
        label="发送人"
        prop="createUserName"
        show-overflow-tooltip
      />
      <el-table-column
        label="发送时间"
        prop="createTime"
        show-overflow-tooltip
      />
      <el-table-column
        label="回复内容"
        prop="repeatContent"
        width="140"
        show-overflow-tooltip
      />
      <el-table-column
        label="回复时间"
        prop="repeatTime"
        show-overflow-tooltip
      />
      <el-table-column
        label="状态"
        prop="status"
        show-overflow-tooltip
      />
      <el-table-column
        label="操作"
        show-overflow-tooltip
        width="240"
      >
        <template slot-scope="scope">
          <el-button
            @click="viewProject(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            >查看</el-button
          >
          <!-- <el-button
            @click="editProject(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            v-has="'leaderApprove:edit'"
            >编辑</el-button
          > -->
          <el-button
            @click="replayProject(scope.row)"
            type="text"
            style="color: #00baff; font-weight: 400"
            v-has="'leaderApprove:edit'"
            >回复</el-button
          >
          <el-button
            @click="deleteProject(scope.row)"
            type="text"
            style="color: #ff5b5b; font-weight: 400"
            v-has="'leaderApprove:delete'"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination" v-if="total">
      <el-pagination
        background
        layout="prev, pager, next"
        :page-size="parameter.pageSize"
        @current-change="pageChange"
        :total="total"
        :current-page="parameter.pageIndex"
      >
      </el-pagination>
    </div>

    <AddEnterpriseDialog
      :visible.sync="showDialog"
      :title="title"
      :currentData="currentData"
      :current="current"
      :isDetail="isDetail"
      @confirm="handleSubmit"
    />
  </div>
</template>

<script>
import {
  queryLeaderApproval,
  leaderDelete,
  leaderReply
} from '@/api/dataManagement'
import Icon from '@/components/icon'
import {
  leaderStatusOptions,
  leaderStatusModalOptions
} from '@/utils/project/data'
import AddEnterpriseDialog from './components/addEnterpriseDialog.vue'
import dayjs from 'dayjs'
import listKeepAliveMixin from '@/mixins/listKeepAliveMixin'

export default {
  name: 'leaderApproveList',
  mixins: [listKeepAliveMixin],
  components: {
    Icon,
    AddEnterpriseDialog
  },
  data() {
    return {
      projectList: [],
      total: '',
      parameter: {
        keywords: '',
        pageIndex: 1,
        pageSize: 10
      },
      projectTypeOptions: [],
      loading: false,
      showDialog: false,
      currentData: {
        pkid: '',
        year: '',
        enterpriseName: '',
        hasStandard: '是',
        hasTechnology: '是',
        hasMastery: '是',
        planCompleteTime: '',
        completeTime: '',
        contract: ''
      },
      isDetail: false,
      title: '',
      current: ''
    }
  },
  computed: {
    leaderStatusOptions() {
      return leaderStatusOptions
    },
    leaderStatusModalOptions() {
      return leaderStatusModalOptions
    }
  },
  mounted() {
    this.getList()
  },
  activated() {
    if (this.$route.query.refresh) {
      this.getList()
      this.$router.replace({
        path: this.$route.path,
        query: { ...this.$route.query, refresh: undefined }
      })
    }
  },

  methods: {
    search() {
      this.parameter.keywords =
        this.parameter.keywords?.trim?.() || ''
      this.parameter.pageIndex = 1
      this.getList()
    },

    pageChange(page) {
      this.parameter.pageIndex = page
      this.getList()
    },

    getList() {
      this.loading = true
      queryLeaderApproval(this.parameter).then((res) => {
        if (res.success) {
          this.total = res.data.count
          this.projectList = res.data.list.map((item) => {
            return {
              ...item,
              status: this.leaderStatusModalOptions.filter(
                (i) => i.value === Number(item.status)
              )[0].label
            }
          })
        }
        this.loading = false
      })
    },

    // 表格高亮
    tableRowClassName({ rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'light'
      } else {
        return ''
      }
    },

    // 新增
    addProject() {
      this.showDialog = true
      this.isDetail = false
      this.currentData = Object.assign(
        {},
        this.$options.data().currentData
      )
      this.title = '新增'
      this.current = 'add'
    },

    // 编辑
    editProject(row) {
      this.showDialog = true
      this.currentData = row
      this.isDetail = false
      this.title = '编辑'
      this.current = 'edit'
    },

    // 回复
    replayProject(row) {
      this.currentData = row
      this.showDialog = true
      this.isDetail = true
      this.title = '回复'
      this.current = 'reply'
    },

    // 项目查看
    viewProject(row) {
      this.currentData = row
      this.showDialog = true
      this.isDetail = true
      this.title = '查看'
      this.current = 'view'
    },

    // 删除项目
    deleteProject(row) {
      this.$confirm(
        '此操作将永久删除该交办，是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          leaderDelete(row.pkid).then((res) => {
            if (res.success) {
              this.$message.success('删除成功')
              if (
                this.total % 10 === 1 &&
                this.parameter.pageIndex ===
                  parseInt(this.total / 10) + 1 &&
                this.parameter.pageIndex !== 1
              ) {
                this.parameter.pageIndex -= 1
              }
              this.getList()
            } else {
              this.$message.error(res.message)
            }
          })
        })
        .catch((error) => {
          this.$message.info('已取消删除')
        })
    },
    downloadFile() {
      if (!this.parameter.year) {
        this.$message.warning('导出请先选择年份')
        return
      }
      this.btnLoading = true
      downloadProject(this.parameter).then((res) => {
        const blob = new Blob([res])
        const a = document.createElement('a')
        a.href = URL.createObjectURL(blob)
        a.download = '固投目标作战图.xlsx'
        a.style.display = 'none'
        document.body.appendChild(a)
        a.click()
        a.remove()
        this.btnLoading = false
      })
    },
    handleSubmit(reply) {
      this.currentData.repeatContent = reply
      leaderReply(this.currentData).then((res) => {
        if (res.success) {
          this.$message.success('回复成功')
          this.showDialog = false
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
@import '~@/assets/css/table.scss';
</style>
